package com.facishare.open.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.facishare.open.ajax.code.AjaxCode;
import com.facishare.open.ajax.result.AjaxResult;
import com.facishare.open.common.result.exception.BizException;
import com.facishare.open.entity.User;
import com.facishare.open.service.UserService;
import com.google.gson.JsonArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.List;

/**
 * Created by linchf on 2016/7/22.
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    @RequestMapping("/insert")
    public AjaxResult insert(User user) {
        log.info("com.facishare.open.controller.UserController.insert()");
        try{
            int num = userService.insert(user);
            if(num <= 0)
                throw new Exception("插入失败");
        }catch (Exception e) {
            return new AjaxResult(AjaxCode.BIZ_EXCEPTION,e.toString());
        }
        return SUCCESS;
    }

    @RequestMapping("/delete")
    public AjaxResult delete(User user) {
        log.info("com.facishare.open.controller.UserController.delete()");
        try{
            int num = userService.delete(user);
            if(num <= 0)
                throw new Exception("删除失败！");
        }catch (Exception e) {
            return new AjaxResult(AjaxCode.BIZ_EXCEPTION,e.toString());
        }

        return SUCCESS;
    }

    @RequestMapping("/update")
    public AjaxResult update(User user,HttpSession session) {
        log.info("com.facishare.open.controller.UserController.update()");

//        User u = (User)session.getAttribute("user");
//        log.info(u.getUsername()+","+u.getPwd());

        try {
            int num = userService.update(user);
            if(num <= 0)
                throw new Exception("更新失败！");
        }catch (Exception e) {
            return new AjaxResult(AjaxCode.BIZ_EXCEPTION,e.toString());
        }

        return SUCCESS;
    }

    @RequestMapping("/search")
    public AjaxResult search(User user) {
        log.info("com.facishare.open.controller.UserController.search()");
        try{
            user = userService.search(user);
        }catch (Exception e) {
            return new AjaxResult(AjaxCode.BIZ_EXCEPTION,e.toString());
        }

        return new AjaxResult(user);
    }

    @RequestMapping("/searchLike")
    public AjaxResult searchLike(User user) {
        log.info("com.facishare.open.controller.UserController.searchLike()");
        String data = "";
        try{
            List<User> uList = userService.searchLike(user);
//          data = JSONArray.toJSONString(uList);
            if(uList.size()>0) {
                for(int i = 0;i<uList.size();i++) {
                    log.info(uList.get(i)+"");
                    data+=uList.get(i)+",";
                }
                data = data.substring(0,data.length()-1);
            }
        }catch (Exception e) {
            return new AjaxResult(AjaxCode.BIZ_EXCEPTION,e.toString());
        }

       return new AjaxResult(data);
    }

    @RequestMapping("/login")
    public AjaxResult login(User user, HttpSession session) {
        log.info("com.facishare.open.controller.UserController.login()");

        try{
            User u = userService.search(user);

            if(u != null && u.getPwd().equals(user.getPwd())) {
                session.setAttribute("user",user);
            }
            else {
                throw new Exception("账号密码错误！");
            }
        }catch (Exception e) {
            return new AjaxResult(AjaxCode.BIZ_EXCEPTION,e.toString());
        }

        return SUCCESS;
    }

}
